// 主界面
;
define(function(require, exports, module) {
	'use strict';
	var util = require("util");
	var base = require("base");
	var router = require("../hash/pageRouter");
	var css_detail_info = require("../../../html/platform/financing/financingDetailInfo.css");
	var css_detail_info1 = require("../../../html/platform/financing/financingManage.css");
	var financeFlag;

	var dialog = require("dialog");
	var validate = require("validate");
	var databind = require("databind");
	var constant = require("constant");
	var pageIndex = 1,
		pageSize = 10000,
		totalPageCount = 1,
		totalRecordCount = 0; //分页初始化
	var deliveryNoticeId;
	var noticeState;
	var deliveryNoticeDetail;
	var clerk;
	var financeId;
	var init = function(page) {
		financeId = page.params.financeId || base.getUrlParam("financeId");
		deliveryNoticeId = page.params.deliveryNoticeId || base.getUrlParam("deliveryNoticeId");
		noticeState = page.params.noticeState || base.getUrlParam("noticeState");

		if (noticeState == 1) {
			$("#acceptTasks").removeClass("none");
			$("#signDelivery").addClass("none");
		} else if (noticeState == 2) {
			$("#acceptTasks").addClass("none");
			$("#signDelivery").removeClass("none");
		} else {
			$("#acceptTasks").addClass("none");
			$("#signDelivery").addClass("none");
		}
		initEvent();
		inquireDeliveryPageList();
		router.init();
	};

	var initEvent = function() {
		//若未登录，则返回登陆页面
		if (base.getData("sid") == null || base.getData("sid") == "" || base.getData("sid") == undefined) {
			top.location.href = "../login/login.html";
		}

		//解除监管 
		$(".deliveryTable").on("click", ".createSupervisionRelieve", function() {

			var deliveryId = [];
			var $this = $(this).parents("tr.tr_style");
			deliveryId.push($this.attr("deliveryId"));
			dialog.showConfirmDialog("解除监管", "是否要解除监管？", function() {
				base.fnAjax({
					action: "createSupervisionRelieve.json",
					oData: {
						deliveryId: deliveryId
					}, //传递参数
					fnSuccess: function(data) {
						if (data.status == 200) {
							base.alert(1, "解除监管成功");
//							inquireFinanceApplicationDetail();
//							if(financeFlag){
//								createSupervisionEndall();
//							}
						} else {
							base.alert(2, "信息获取失败");
						}
					}
				});
			});
		});

		$(".page-content-main").on("click", ".handle-type", function() {
			var deliveryNoticeId = [];
			var $this = $(this).parents("tr.tr_style");
			deliveryNoticeId.push($this.attr("deliveryNoticeId"));
			router.route("financing/financingDetail_signDeliveryList.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
		});

		//点击放货配送
		$(".deliveryList").on("click", function() {
			router.route("deliveryNotice/deliveryList.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);

		});

		//点击签收
		$(".deliveryTable").on("click", ".signDelivery", function() {

			var deliveryId = [];
			var $this = $(this).parents("tr.tr_style");
			deliveryId.push($this.attr("deliveryId"));
			dialog.showDialog($("#signDelivery_dialog"), function() {});
			databind.fillForm($("#signDelivery_dialog"), {
				deliveryId: deliveryId
			});
			$("#sure_signDelivery").on("click", function() {

				databind.submitDom($(".signDeliveryContent"), function() {
					base.alert(1, "签收放货配送成功");
					dialog.hideDialog($(".signDeliveryContent"));
					router.route("deliveryNotice/deliveryList.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
				});
			});

		});

		//点击接受放货任务
		$("#acceptTasks").on("click", function() {
			dialog.showConfirmDialog("接受放货任务", "是否要接受此放货任务？", function() {

				base.fnAjax({
					action: "confirmDeliveryNotice.json",
					oData: {
						deliveryNoticeId: deliveryNoticeId
					}, // 传递参数
					fnSuccess: function(data) {
						if (data.status == 200) {
							base.alert(1, "接受放货任务成功");
						} else {
							base.alert(2, "信息获取失败");
						}
					}
				});
			});
		});

		$("#clerkName").on("change", function() {
			$(".clerkNameInput").val($("#clerkName  option:selected").text());
			var clerkId = $("#clerkName").val();
			$.each(clerk.datas, function(i, val) {
				if (val.clerkId == clerkId) {
					$("#clerkMobile").val(val.clerkMobile);
				}
			});
		});

		//signDelivery点击生成放货配送
		$("#signDelivery").on("click", function() {
			dialog.showDialog($("#delivery_dialog"), function() {});
			inquireLogisticsClerkPageList();
			showInventory();
		});

		//点击下发放货配送提交按钮
		$("#delivery_dialog").on("click", "#sure_deliveryNotice", function() {
			var inventoryIds = [];
			$.each($(".inventoryId:checked"), function(i, val) {
				inventoryIds[i] = val.value;
			});
			if (inventoryIds.length == 0) {
				base.alert(2, "请勾选货箱再下达放货配送！");
				return;
			}
			var clerkName = $(".clerkNameInput").val();
			var clerkId = $("#clerkName").val();
			var clerkMobile = $("#clerkMobile").val();
			base.fnAjax({
				action: "createDelivery.json",
				oData: {
					deliveryNoticeId: deliveryNoticeId,
					clerkMobile: clerkMobile,
					clerkId: clerkId,
					clerkName: clerkName,
					inventoryIds: inventoryIds
				}, //传递参数
				fnSuccess: function(data) {
					if (data.status == 200) {
						base.alert(1, "下达放货配送成功");
						dialog.hideDialog($("#delivery_dialog"));
						router.route("deliveryNotice/deliveryNoticeDetail.html?deliveryNoticeId=" + deliveryNoticeId + "&noticeState=" + noticeState, true);
					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});

		});
	};
	
	var inquireFinanceApplicationDetail = function() {
		base.fnAjax({
			action: "inquireFinanceApplicationDetail.json",
			oData: {
				financeId: financeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var bs = data.datas[0].bulkstocks;
					for(var j=0; j<bs.length; j++){
						if(bs[j].incustody == bs[j].pickup){
							financeFlag = true;
						}else{
							financeFlag = false;
							return;
						}
					}
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};
	
	//完成融资
	var createSupervisionEndall = function() {
		base.fnAjax({
			action: "createSupervisionEndall.json",
			oData: {
				financeId: financeId,
				remark: "融资完成"
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					base.alert(1, "融资完成");
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	};

	// 查询收货通知列表
	var inquireDeliveryNoticePageList = function() {
		var body = base.parseForm($(".page-content-search"));
		var params = {
			pageIndex: pageIndex,
			pageSize: pageSize,
			financeId: financeId
		}
		base.apply(params, body);

		base.fnAjax({
			action: "inquireDeliveryNoticePageList.json",
			oData: params, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#deliveryNoticeListTpl").html();
					var html = juicer(tpl, data);
					$("#deliveryNoticeListBody").html(html);
					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#pagenation"), totalRecordCount, data.pageIndex, data.pageSize, function(p) {
						pageIndex = p;
						inquireDeliveryNoticePageList();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	//显示运输通知--货箱
	var showInventory = function() {
		base.fnAjax({
			action: "inquireWaitDeliveryBulkstockInventoryList.json",
			oData: {
				deliveryNoticeId: deliveryNoticeId
			}, // 传递参数
			fnSuccess: function(data) {
				if (data.status == 200) {
					var tpl = $("#inventoryTpl").html();
					var html = juicer(tpl, data);
					$("#inventoryBody").html(html);
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});


	}
	var inquireLogisticsClerkPageList = function() {
			base.fnAjax({
				action: "inquireLogisticsClerkPageList.json",
				oData: {
					pageIndex: pageIndex,
					pageSize: pageSize
				}, // 传递参数
				fnSuccess: function(data) {
					clerk = data;
					if (data.status == 200) {
						var tpl = $("#clerkTpl").html();
						var html = juicer(tpl, data);
						$("#clerkName").html(html);

					} else {
						base.alert(2, "信息获取失败");
					}
				}
			});
		}
		//获取放货通知明细(放货通知)
	var inquireDeliveryPageList = function() {
		base.fnAjax({
			action: "inquireDeliveryPageList.json",
			oData: {
				pageIndex: pageIndex,
				pageSize: pageSize
			}, // 传递参数
			fnSuccess: function(data) {
				deliveryNoticeDetail = data;
				if (data.status == 200) {
					var tpl = $("#deliveryListTpl").html();
					var html = juicer(tpl, data);
					$("#deliveryListBody").html(html);

					totalPageCount = data.totalPageCount;
					totalRecordCount = data.totalRecordCount;
					base.pageControl($("#pagenation"), totalRecordCount, data.pageIndex, data.pageSize, function(p) {
						pageIndex = p;
						inquireDeliveryPageList();
					});
				} else {
					base.alert(2, "信息获取失败");
				}
			}
		});
	}

	return {
		init: init
	};
});